package com.phone580.app;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

import com.phone580.app.entity.DownloadInfoEntity;

public class DownloadWorker implements Runnable {
	private Object lock = new Object();
	private boolean done = false;
	private URL httpurl;
	private HttpURLConnection connection;
	private RandomAccessFile randomAccessFile;
	private InputStream in;
	private  DownloadInfoEntity info;
//	private DownloadCallback callback;
	
	public DownloadWorker(DownloadInfoEntity info, DownloadCallback callback) {
		this.info = info;
//		this.callback = callback;
		try {
			System.out.println(info.getFileName() + "-----standby");
			httpurl = new URL(info.getUrl());
			connection = (HttpURLConnection) httpurl.openConnection();
			connection.setConnectTimeout(5000);
			connection.setRequestMethod("GET");
			if (info.getCompleteSize() != 0) {
				connection.setRequestProperty(
						"Range",
						"bytes=" + info.getCompleteSize());
			}
			randomAccessFile = new RandomAccessFile(info.getFilePath(),
					"rwd");
			randomAccessFile.seek(info.getCompleteSize());
		} catch (MalformedURLException e) {
			e.printStackTrace();
			System.out.println(info.getFileName() + "-----error");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(info.getFileName() + "-----error");
		}
	}
	
	@Override
	public void run() {
		Assignment assign = new Assignment();
		synchronized (lock) {
			assign.start();
			while (!done) {
				try {
					lock.wait();
					System.out.println(info.getFileName() + "-----finish");
				} catch (InterruptedException e) {
					done = true;
					try {
						connection.disconnect();
						randomAccessFile.close();
						if (in != null) {
							in.close();
						}
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}
				} finally {
					connection.disconnect();
					try {
						randomAccessFile.close();
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
			}
		}
	}
	
	class Assignment extends Thread{

		@Override
		public void run() {
			try {
				if (done) {
					return;
				}
				System.out.println(info.getFileName() + "-----downloading");
//				if (info.getFileSize() == 0) {
//					int size = connection.getContentLength();
//					info.setFileSize(size);
//				}
				in = connection.getInputStream();
				byte[] buffer = new byte[4096];
				int length = -1;
				int completeSize = info.getCompleteSize();
				while ((length = in.read(buffer)) != -1) {
					if (length == 0) {
						continue;
					}
					randomAccessFile.write(buffer, 0, length);
					completeSize += length;
					info.setCompleteSize(completeSize);
					Log.d("download", info.getFileName() + "-----" + completeSize);
				}
//				callback.success(info);
				System.out.println(info.getFileName() + "-----done");
			} catch (IOException e) {
				System.out.println(info.getFileName() + "-----pause");
//				callback.pause(info);
				done = true;
			} finally {
				synchronized (lock) {
					System.out.println("notify threadid:" + getId());
					lock.notify();
				}
			}
		}
		
	}

}
